package com.zhang.chatcenter.common.generator;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * 逆向工程生成的entity实体类中id注解为auto，需要修改为input
 *
 * @author zfj
 * @date 2022/3/31
 */
public class CodeGenerator {

//    private static String url = "jdbc:mysql://10.88.130.97:3308/business_authority_center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
    private static String url = "jdbc:mysql://10.88.130.97:3308/business_product_center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String username = "root";
    private static String password = "root";
    private static String file_path = "D:/code/business-center";

    /**
     * 表名，多个英文逗号分割，如果是全表，就为null
     */
    private static String[] tableName = new String[]{"user_search"};
//    private static String[] tableName = null;

    private static final String entityPath = "com.zhang.product";
    private static final String xmlPah = "/src/main/resources/mapper";

    public static void main(String[] args) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("KingHood") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(file_path); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent(entityPath) // 设置父包名
                            .moduleName("core") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, file_path)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder
                            .addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix("vvv_", "c_") // 设置过滤表前缀
                            .controllerBuilder().enableFileOverride().enableRestStyle()
                            .serviceBuilder().enableFileOverride()
                            .entityBuilder().enableFileOverride().enableLombok().enableChainModel().enableTableFieldAnnotation()
                            .mapperBuilder().enableFileOverride();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
